//
//  Soap.m
//  TestConnection
//
//  Created by Boc on 8/8/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "Soap.h"
#import "XMLParserDelegate.h"
//#import "XMLReader.h"
@implementation Soap

@synthesize delegate;

-(void) makeConnection
{
    soapMessage = @"soapMessage";

    NSURL *url = [NSURL URLWithString:@"http://dl.dropbox.com/u/27234076/movies.xml"];
    NSMutableURLRequest * theRequest = [NSMutableURLRequest requestWithURL:url];
//    NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMessage length]];
//    
//    [theRequest addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
//    [theRequest addValue:@"http://viium.com/Hello" forHTTPHeaderField:@"SOAPActon"];
//    [theRequest addValue:msgLength forHTTPHeaderField:@"Content-Type"];
//    [theRequest setHTTPMethod:@"POST"];
//    [theRequest setHTTPBody:[soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
//    
    theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

    webData =[[NSMutableData alloc] init];
    
    NSLog(@"makeConnection");
}

-(id) init
{
    self = [super init];
    
    return self;
}

-(id) initConnection
{
    self = [super init];
    
    if (self != nil) {
        [self makeConnection];
    }
    return self;
}
-(void)dealloc
{
    [super dealloc];
    /*
    if (webData)
    {
        [webData release];
    }
    
    if (xmlParser)
    {
        [xmlParser release];
    }
    
    if (_xmlParserDelegate)
    {
        [_xmlParserDelegate release]; 
    }
    
    if (theConnection)
    {
        [theConnection autorelease];
    }
    */

}
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    [webData setLength:0]; 
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [webData appendData:data];
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    [connection release];
    [webData release];
    
    NSLog(@"Connection error: %@", [error localizedDescription]);
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSLog(@"All data recived");
    //NSString *theXML = [[NSString alloc] initWithBytes:[webData mutableBytes] length:[webData length] encoding:NSUTF8StringEncoding];
    //NSLog(theXML);
    
    if (xmlParser)
    {
        [xmlParser release];
    }
    
    if (_xmlParserDelegate) {
        [_xmlParserDelegate release]; 
    }
 /*   NSDictionary *xmlDic = [XMLReader dictionaryForXMLData:webData error:nil];
    
    NSLog(@"Dictionary count: %d", [xmlDic count]);
    NSLog([xmlDic allKeys]);
    NSLog([xmlDic allValues]);
  */
    _xmlParserDelegate = [[XMLParserDelegate alloc]init];
    xmlParser = [[NSXMLParser alloc] initWithData:webData];
    _xmlParserDelegate.delegate = delegate;
    [xmlParser setDelegate:_xmlParserDelegate];
    [xmlParser setShouldResolveExternalEntities:YES];
    [xmlParser parse];

    
    [connection release];
    [webData release];
}
@end
